package baseFx.tests.common.mq.mqbean;

import baseFx.common.context.BeanDef;
import baseFx.common.context.TypeResolver;
import baseFx.common.mq.*;
import baseFx.common.mq.impl.AppMqPublisher;
import baseFx.common.mq.impl.AppMqSubscriber;
import baseFx.common.utils.ClassUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Scanner;
import java.util.UUID;

public class Run {
    public static class Bean implements MqBean, Serializable {
        public String id;
    }

    public static class Bean2 implements MqBean, Serializable {
        public String name;
    }

    public interface A extends MqBeanSubscriber {
        void a1(Bean bean);

        void a2(Bean2 bean);
    }

    public interface A2 extends MqBeanSubscriber {
        void a3(Bean bean);
    }

    public static class B implements A, A2 {
        @Override
        public void a1(Bean bean) {
            System.out.println("id= " + bean.id);
        }

        @Override
        public void a2(Bean2 bean) {
            System.out.println("name= " + bean.name);
        }

        @Override
        public void a3(Bean bean) {
            System.out.println("a3= " + bean.id);
        }
    }

    public static class C implements A2 {

        @Override
        public void a3(Bean bean) {
            System.out.println("C= " + bean.id);
        }
    }

    public static void main(String[] args) {
        MqSubscriber subscriber = new AppMqSubscriber();
        MqPublisher publisher = new AppMqPublisher();
        DefaultMqBeanDispatcher dispatcher = new DefaultMqBeanDispatcher(new TypeResolver() {
            @Override
            public TypeResolver root() {
                return this;
            }

            @Override
            public TypeResolver sub(BeanDef... params) {
                return this;
            }

            @Override
            public void register(BeanDef def) {

            }

            @Override
            public <T> T resolve(Class<T> clazz) {
                if (MqPublisher.class == clazz) {
                    return (T) publisher;
                }
                if (MqSubscriber.class == clazz) {
                    return (T) subscriber;
                }
                return ClassUtils.newInstance(clazz);
            }

            @Override
            public List<Class<?>> getClassListForType(Class<?> clazz) {
                return null;
            }

            @Override
            public void close() {

            }
        });
        dispatcher.register(B.class);
        dispatcher.register(C.class);
        dispatcher.start();

        Scanner scanner = new Scanner(System.in);
        String input = "";
        while ("0".equals((input = scanner.nextLine())) == false) {
            if ("1".equals(input)) {
                Bean b = new Bean();
                b.id = UUID.randomUUID().toString();
                dispatcher.push(b);
            } else if ("2".equals(input)) {
                Bean2 b = new Bean2();
                b.name = System.currentTimeMillis() + "";
                dispatcher.push(b);
            }
        }
        dispatcher.close();
        System.out.println("exit");
    }
}
